/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.web.formmetadata.i18n.component.strategy.i18nresourcestrategy;

import com.inspur.edp.lcm.metadata.api.entity.I18nResourceItem;
import com.inspur.edp.lcm.metadata.api.entity.I18nResourceItemCollection;
import com.inspur.edp.web.common.utility.StringUtility;
import com.inspur.edp.web.formmetadata.i18n.I18nResourceItemManager;
import com.inspur.edp.web.formmetadata.i18n.component.ComponentUtility;
import com.inspur.edp.web.formmetadata.i18n.component.strategy.i18nresourcestrategy.CommonStrategy.GridCommonStrategy;
import com.inspur.edp.web.formmetadata.i18n.constant.I18nResourceConstant;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * datagrid 国际化参数日趋策略
 *
 * @author noah
 */
public class DataGridI18nResourceStrategy extends GridCommonStrategy {
    /**
     * 获取组件属性中多语资源项集合
     *
     * @param i18nResourceItemBaseId
     * @param currentComponent
     * @return
     */
    @Override
    protected I18nResourceItemCollection extractAttributeI18nResourceItemCollection(String i18nResourceItemBaseId, HashMap<String, Object> currentComponent) {
        I18nResourceItemCollection i18nResourceItemCollection = new I18nResourceItemCollection();

        // 从lineNumberTitle属性中提取多语字段
        I18nResourceItemCollection LineNumberTitleI18nResourceItemCollection = ExtractComponentLineNumberTitleI18nResourceItemCollection(i18nResourceItemBaseId, currentComponent);
        if (LineNumberTitleI18nResourceItemCollection != null && LineNumberTitleI18nResourceItemCollection.size() > 0) {
            i18nResourceItemCollection.addRange(LineNumberTitleI18nResourceItemCollection);
        }

        // 从headerGroup属性中提取多语字段
        String headerGroupI18nResourceItemBaseId = ComponentUtility.getInstance().getType(currentComponent) + I18nResourceConstant.SECOND_LEVEL_DELIMITER + ComponentUtility.getInstance().getId(currentComponent) + I18nResourceConstant.SECOND_LEVEL_DELIMITER;
        I18nResourceItemCollection headerGroupI18nResourceItemCollection = ExtractHeaderGroupResourceItemCollection(i18nResourceItemBaseId, headerGroupI18nResourceItemBaseId, currentComponent);
        if (headerGroupI18nResourceItemCollection != null && headerGroupI18nResourceItemCollection.size() > 0) {
            i18nResourceItemCollection.addRange(headerGroupI18nResourceItemCollection);
        }

        //提取grid 的操作列
        I18nResourceItemCollection operateButtonResourceItemCollection = ExtractComponentOperateButtonI18nResourceItemCollection(i18nResourceItemBaseId, currentComponent);
        if (operateButtonResourceItemCollection != null && operateButtonResourceItemCollection.size() > 0) {
            i18nResourceItemCollection.addRange(operateButtonResourceItemCollection);
        }

        // 提取上下问菜单中的title参数
        I18nResourceItemCollection contextMenuResourceItemCollection = extractContextMenu(i18nResourceItemBaseId, currentComponent);
        if (contextMenuResourceItemCollection != null && contextMenuResourceItemCollection.size() > 0) {
            i18nResourceItemCollection.addRange(contextMenuResourceItemCollection);
        }

        //提取columns里的行内容
        if(currentComponent.containsKey("columns")){
            I18nResourceItemCollection columnsI18nResourceItemCollection=extractedColumns( i18nResourceItemBaseId, currentComponent);
            if (columnsI18nResourceItemCollection != null && columnsI18nResourceItemCollection.size() > 0) {
                i18nResourceItemCollection.addRange(columnsI18nResourceItemCollection);
            }
        }
        //提取handing内的行号值
        if(currentComponent.containsKey("rowNumber")){
            String currentComponentType = ComponentUtility.getInstance().getType(currentComponent);
            String currentComponentId = ComponentUtility.getInstance().getId(currentComponent);
            HashMap<String,Object>rowNumber= (HashMap<String, Object>) currentComponent.get("rowNumber");
            String heading= (String) rowNumber.get("heading");
            String generatedComponentId =  currentComponentId + "/rowNumber/heading";
            I18nResourceItem footerButtonI18nResourceItem = I18nResourceItemManager.createI18nResourceItem(i18nResourceItemBaseId, generatedComponentId, heading, heading);
            this.addInCollection(i18nResourceItemCollection, footerButtonI18nResourceItem);
        }

        return i18nResourceItemCollection;
    }


}
